package com.spark.lola.hander;

import com.alibaba.fastjson.JSONObject;
import com.spark.lola.feign.service.PortalServiceOpenApi;
import com.spark.lola.request.LoginRequest;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.netty.api.NettyPermissionService;
import com.spark.lola.support.wms.cache.CollectionCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @Author: DengYeJun
 * @Date: 2019/5/15 0015 18:36
 */
@Qualifier("nettyPermissionService")
@Service
public class NettyPermissionServiceImpl implements NettyPermissionService {
    @Autowired
    private PortalServiceOpenApi portalServiceOpenApi;
    @Override
    public boolean checkSession(String channelId) {
        return CollectionCache.managers.containsKey(channelId);
    }

    @Override
    public String getUserId(String channelId) {
        if (CollectionCache.managers.containsKey(channelId)) {
            return CollectionCache.managers.get(channelId).getId().toString();
        }
        return null;
    }

    /**
     * 激活续期会员
     * @param channelId
     */
    @Override
    public void touch(String channelId) {

    }

    @Override
    public void removeRelation(String channelId) {
        CollectionCache.managers.remove(channelId);
    }

    @Override
    public String login(String channelId, String ipAddress, byte[] data) {
        LoginRequest request = JSONObject.parseObject(new String(data),LoginRequest.class);
        Long userId = Long.valueOf(request.getUserName());
        if(CollectionCache.employees.containsKey(userId)) {
            // 登陆成功
            ShiroUser shiroUser = CollectionCache.employees.get(userId);
            CollectionCache.managers.put(channelId, shiroUser);
            return shiroUser.getLoginNo();
        }
        return "500~登陆失败,用户名或密码错误!";
    }
}
